var $ = require('../../utils/Ajax.js');

Page({
  data: {
    StarsIndex: 5,
    Stars: 5,
    files: [],
    ClickUploader: false,
    EvaluationPlace: "",
    Id: null,
    ServerFiles: [],
    TopImgLenght: 0,
    ImgContNum: 6,
    Type:null
  },
  onLoad: function (options) {
    console.log(options.Id)
    console.log(options.Type)
    wx.setNavigationBarTitle({
      title: "评价"
    })
    this.setData({
      Id: options.Id,
      Type: options.Type
    })
  },
  // 点击五角星
  ClickStars: function (e) {
    this.setData({
      Stars: e.currentTarget.dataset.index + 1
    })
  },
  // 上传图片
  chooseImage: function (e) {
    var that = this;
    var ImgContNum = that.data.ImgContNum;
    wx.chooseImage({
      count: ImgContNum,
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 将每次上传图片的路径追加到files数组里面
        that.setData({
          files: that.data.files.concat(res.tempFilePaths)
        });
        // 总共可以上传6张图片减去本次上传的数量,就等于下次可以上传的数量
        var ImgNum = 6 - that.data.files.length;
        that.setData({
          ImgContNum: ImgNum
        })
        // 判断以经上传的照片大于或等于6张 就隐藏添加图片按钮
        if (that.data.files.length >= 6) {
          that.setData({
            ClickUploader: true
          });
        }
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 10000
        })
        // 默认第一次上传图片的时候 I 等于0
        // 第二次上传的时候 I 就等于上次上传图片的数量
        for (var i = that.data.TopImgLenght; i < that.data.files.length; i++) {
          wx.uploadFile({
            url: 'https://xcx.folomeet.com/api/x_upload',
            filePath: that.data.files[i],
            name: 'file',
            success: function (res) {
              wx.hideToast();
              that.setData({
                ServerFiles: that.data.ServerFiles.concat(JSON.parse(res.data).filename)
              })
              wx.showToast({
                title: '上传成功',
                icon: 'success',
                duration: 1500
              });
            },
            fail: function (res) {
              wx.hideToast();
              wx.showModal({
                title: '错误提示',
                content: '上传图片失败',
                showCancel: false,
                success: function (res) { }
              })
            }
          });
        }
        // 将图片的数量赋给 TopImgLenght 以便第二次上传图片的 I
        that.setData({
          TopImgLenght: that.data.files.length
        })
      }
    })
  },

  // 单击图片放大
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },

  // 评论内容
  EvaluationPlace: function (e){
    this.data.EvaluationPlace = e.detail.value;
  },
  
  // 提交评论
  UsedBtn: function (){
    var that = this;
    var Type = that.data.Type;
    $.Ajax("api/x_add_com", "POST", {
      content: that.data.EvaluationPlace,
      star: that.data.Stars,
      type: Type,
      order_id: Number(that.data.Id),
      imgs: that.data.ServerFiles
    }, function (res) {
      console.log(res)
      if (res.s) {
        wx.showToast({
          title: res.m,
          icon: 'success',
          duration: 1500
        })
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.m,
          icon: 'success',
          image: '../../images/err.png',
          duration: 1500
        })
      }
    });
  }
});